"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
  class Course extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      /**
       * belongsTo：当前实体类属于另一个实体类
       * hasMany：当前实体类有多个其他子实体类
       * belongsToMany：当前实体有多个子实体类，当前实体类也属于另一个实体类的子类
       */
      models.Course.belongsTo(models.Category, { as: "category" });
      models.Course.belongsTo(models.User, { as: "user" });
      models.Course.hasMany(models.Chapters,{as:'chapters'})
      models.Course.belongsToMany(models.User,{through:models.Likes,foreignKey:'courseId',as:'likeUsers'})
    }
  }
  Course.init(
    {
      categoryId: {
        type: DataTypes.INTEGER,
        allowNull: false,
        validate: {
          notNull: { msg: "分类ID必须填写。" },
          notEmpty: { msg: "分类ID不能为空。" },
          isInt: { msg: "分类ID必须是整数。" },
          async isPresent(value) {
            const category = await sequelize.models.Category.findByPk(value);
            if (!category) {
              throw new Error("分类ID不存在。");
            }
          },
        },
      },
      userId: {
        type: DataTypes.INTEGER,
        allowNull: false,
        validate: {
          notNull: { msg: "用户ID必须填写。" },
          notEmpty: { msg: "用户ID不能为空。" },
          async isPresent(value) {
            const user = await sequelize.models.User.findByPk(value);
            if (!user) {
              throw new Error("用户ID不存在。");
            }
          },
        },
      },
      name: {
        type: DataTypes.STRING,
        allowNull: false,
        validate: {
          notNull: { msg: "课程名称必须填写。" },
          notEmpty: { msg: "课程名称不能为空。" },
          len: {
            args: [2, 45],
            msg: "课程名称长度必须在2到45个字符之间。",
          },
        },
      },
      image: {
        type: DataTypes.STRING,
        validate: {
          isUrl: { msg: "课程图片必须是一个有效的URL。" },
        },
      },
      recommended: {
        type: DataTypes.BOOLEAN,
        validate: {
          isIn: {
            args: [[true, false]],
            msg: "推荐状态必须是布尔值。",
          },
        },
      },
      introductory: {
        type: DataTypes.BOOLEAN,
        validate: {
          isIn: {
            args: [[true, false]],
            msg: "简介状态必须是布尔值。",
          },
        },
      },
      content: {
        type: DataTypes.TEXT,
        allowNull: false,
        validate: {
          notNull: { msg: "课程内容必须填写。" },
          notEmpty: { msg: "课程内容不能为空。" },
        },
      },
      likesCount: DataTypes.INTEGER,
      chaptersCount: DataTypes.INTEGER,
      free:{
        type:DataTypes.BOOLEAN,
        validate:{
          isIn:{args:[[true,false]],msg:'是否免费课程的值必须是，推荐：true 不推荐：false'}
        }
      }
    },
    {
      sequelize,
      modelName: "Course",
    }
  );
  return Course;
};
